<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编码" prop="vendCode">
        <el-input
          v-model="queryParams.vendCode"
          placeholder="请输入供应商编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="vendName">
        <el-input
          v-model="queryParams.vendName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.gen_vend_info_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建方式" prop="createFrom">
        <el-select v-model="queryParams.createFrom" placeholder="请选择创建方式" clearable>
          <el-option
            v-for="dict in dict.type.gen_vend_create_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入驻日期" prop="joinedAt">
        <el-date-picker clearable
          v-model="queryParams.joinedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入驻日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:genVendInfoPO:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:genVendInfoPO:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:genVendInfoPO:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="genVendInfoPOList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="供应商编码" align="center" prop="vendCode" />
      <el-table-column label="供应商名称" align="center" prop="vendName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gen_vend_info_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建方式" align="center" prop="createFrom">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gen_vend_create_type" :value="scope.row.createFrom"/>
        </template>
      </el-table-column>
      <el-table-column label="入驻日期" align="center" prop="joinedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="更新人" align="center" prop="updatedBy" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:genVendInfoPO:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:genVendInfoPO:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商主数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="供应商名称" prop="vendName">
          <el-input v-model="form.vendName" placeholder="请输入供应商名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 新增抽屉组件 -->
    <el-drawer
      title="新增注册供应商"
      :visible.sync="drawer"
      direction="rtl"
      size="50%"
      :before-close="handleDrawerClose">
      <el-form ref="drawerForm" :model="form" label-width="100px" class="drawer-form">
        <div class="form-section">
          <div class="section-title">基础信息</div>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="供应商编码">
                <el-input v-model="form.vendCode" disabled style="width: 250px" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="供应商名称">
                <el-input v-model="form.vendName" disabled style="width: 250px" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="创建方式">
                <el-select v-model="form.createFrom" disabled style="width: 250px">
                  <el-option
                    v-for="dict in dict.type.gen_vend_create_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="section-title">公司信息</div>
        </div>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { listGenVendInfoPO, getGenVendInfoPO, delGenVendInfoPO, addGenVendInfoPO, updateGenVendInfoPO } from "@/api/system/genVendInfoPO"

export default {
  name: "GenVendInfoPO",
  dicts: ['gen_vend_info_status', 'gen_vend_create_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商主数据表格数据
      genVendInfoPOList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createdBy: null,
        updatedBy: null,
        createdAt: null,
        updatedAt: null,
        vendCode: null,
        vendName: null,
        status: null,
        createFrom: null,
        joinedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vendName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ]
      },
      // 是否显示抽屉
      drawer: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询供应商主数据列表 */
    getList() {
      this.loading = true
      listGenVendInfoPO(this.queryParams).then(response => {
        this.genVendInfoPOList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createdBy: null,
        updatedBy: null,
        createdAt: null,
        updatedAt: null,
        vendCode: null,
        vendName: null,
        status: null,
        remark: null,
        createFrom: 'INITIATED_BACKGROUND',
        joinedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加供应商主数据"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getGenVendInfoPO(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改供应商主数据"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGenVendInfoPO(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.createFrom = 'INITIATED_BACKGROUND'
            addGenVendInfoPO(this.form).then(response => {
              this.open = false
              this.form = response.data
              this.drawer = true
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除供应商主数据编号为"' + ids + '"的数据项？').then(function() {
        return delGenVendInfoPO(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/genVendInfoPO/export', {
        ...this.queryParams
      }, `genVendInfoPO_${new Date().getTime()}.xlsx`)
    },
    // 抽屉关闭前的回调
    handleDrawerClose(done) {
      this.drawer = false
      done()
    }
  }
}
</script>

<style scoped>
.drawer-form {
  padding: 15px;
}
.drawer-form .el-form-item {
  margin-bottom: 15px;
}
.form-section {
  margin-bottom: 20px;
}
.section-title {
  font-size: 14px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 15px;
  padding-left: 8px;
  border-left: 3px solid #409EFF;
}
</style>
